<?php

/**
 * product admin form
 *
 * @package    trunk
 * @subpackage product
 * @author     Your name here
 */
class ProductAdminForm extends BaseProductForm {
    public function configure() {
        parent::configure();
//        unset($this['product_color_list_list']);

        $this->widgetSchema['product_category_id'] =  new sfWidgetFormDoctrineChoiceNestedSet(array(
                        'model'     => 'productCategory',
                        'add_empty' => true,
        ));
        $this->widgetSchema['body_length'] = new sfWidgetFormChoice(array(
                        'choices'  => array(
                                ''=>'',
                                'DISCOUNTED PRODUCTS'=>'DISCOUNTED PRODUCTS',
                                'BUY 1 GET 1 FREE'=>'BUY 1 GET 1 FREE',
                                'COMING SOON'=>'COMING SOON',
                                "STUDENTS OFFER"=>"STUDENT'S OFFER",
                                'FREEEEEE '=>'FREEEEEE'
                        ),
                        'expanded' => false,

        ));
        $productDetailGroupForm = new BaseForm();

        $i = 0;
        foreach ($this->getObject()->getProductDetailFields() as $productDetail) {
            $productDetailAdminForm = new ProductDetailFieldAdminForm($productDetail);
            $productDetailGroupForm->embedForm('product_detail_'.$i, $productDetailAdminForm);
            $productDetailGroupForm->widgetSchema['product_detail_'.$i]->setLabel('&nbsp;');
            $i++;
        }
        $empty_field = sfConfig::get('app_product_detail_field_numbers');
        if($i) {
            $empty_field = ($empty_field/2);
        }
        for ($j = 1; $j <= $empty_field; $j++) {
            $productDetail = new ProductDetailField();
            $productDetail->Product = $this->getObject();

            $productDetailAdminForm = new ProductDetailFieldAdminForm($productDetail);
            $productDetailGroupForm->embedForm('product_detail_'.$i, $productDetailAdminForm);
            $productDetailGroupForm->widgetSchema['product_detail_'.$i]->setLabel('&nbsp;');
            $i++;
        }

        $this->embedForm('product_detail_group_form', $productDetailGroupForm);


//        $relatedProductColorGroupForm = new BaseForm();
//
//        $i = 0;
//        foreach ($this->getObject()->getRelatedProductColors() as $relatedProductColor) {
//            $relatedProductColorAdminForm = new RelatedProductColorAdminForm($relatedProductColor);
//            $relatedProductColorGroupForm->embedForm('related_product_color_'.$i, $relatedProductColorAdminForm);
//            $relatedProductColorGroupForm->widgetSchema['related_product_color_'.$i]->setLabel(dm::getHelper()->media($relatedProductColor->Image)->size(92,92)->set('.product_color_image'));
//            $i++;
//        }
//        $empty_field = sfConfig::get('app_product_detail_color_numbers');
//        if($i) {
//            $empty_field = ceil($empty_field/2);
//        }
//        for ($j = 1; $j <= $empty_field; $j++) {
//            $relatedProductColor = new RelatedProductColor();
//            $relatedProductColor->Product = $this->getObject();
//
//            $relatedProductColorAdminForm = new RelatedProductColorAdminForm($relatedProductColor);
//            $relatedProductColorGroupForm->embedForm('related_product_color_'.$i, $relatedProductColorAdminForm);
//            $relatedProductColorGroupForm->widgetSchema['related_product_color_'.$i]->setLabel('&nbsp;');
//            $i++;
//        }
//
//        $this->embedForm('related_product_color_group_form', $relatedProductColorGroupForm);
//        $this->mergePostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkRelatedProductColorImage'))) );
    }

    public function checkRelatedProductColorImage($validator, $values) {
        foreach($values['related_product_color_group_form'] as $key =>$product_color_from) {
            if(is_object($product_color_from['image_id_form']['file']) || $product_color_from['id']) {
                continue;
            }
            unset($this->embeddedForms['related_product_color_group_form'][$key], $values['related_product_color_group_form'][$key]);

        }

        return $values;
    }

    protected function createMediaFormForImageId() {
        // get the DmMedia form
        $form = parent::createMediaFormForImageId();

        // choose mime types allowed
        $form->setMimeTypeWhiteList(array(
                'image/jpeg',
                'image/png',
                'image/jpg',
                'image/gif'
        ));
        unset($form['legend'], $form['author'], $form['license']);
        return $form;
    }
}